package com.sand.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sand.Exception.SandException;
import com.sand.dto.Select.UserSelect;
import com.sand.dto.supplier.AddSupplier;
import com.sand.dto.supplier.SupplierDto;
import com.sand.entity.user.Owner;
import com.sand.entity.user.supplier;
import com.sand.entity.user.User;
import com.sand.feign.UserFeign;
import com.sand.mapper.SupplierMapper;
import com.sand.owner.Intentionality;
import com.sand.result.AppHttpCodeEnum;
import com.sand.result.ResponseResult;
import com.sand.service.SupplierService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, supplier> implements SupplierService {

    @Autowired
    private UserFeign userFeign;


    /**
     * 添加供货商
     *
     * @param addSupplier
     * @return
     */
    @Override
    public String add(AddSupplier addSupplier) {
        //获取id
        Integer userid = addSupplier.getUserid();


        if (userid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入用户id");
        }

        //根据id查询用户
        User user = userFeign.getById(userid).getData();


        if (user == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"不存在该用户");
        }

        //保存
        supplier supplier = new supplier();
        supplier.setUserid(userid);

        save(supplier);

        return "新增供货商成功";
    }



    /**
     * 查询供货商
     *
     * @param userSelect
     * @return
     */
    @Override
    public SupplierDto select(UserSelect userSelect) {
        Integer id = userSelect.getId();

        String phone = userSelect.getPhone();

        //两个都没传入
        if (id == null && phone.isEmpty()){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"未传入要查寻的用户id或号码");
        }

        if (id != null && (phone == null || phone.isEmpty())) {

            //根据供货商表id查询
            supplier supplier = getById(id);
            if (supplier == null){
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该客户不存在");
            }

            //获得供货商的userid
            Integer userid = supplier.getUserid();

            //根据电话号码获取人员信息
            ResponseResult<User> inquire = userFeign.inquire(new UserSelect(userid, null));
            if (inquire.getData() == null) {
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该电话号码不存在");
            }

            return getSupplierDto(inquire.getData(), supplier);

        }

        //只传入id
        if (id == null){
            //只传了电话号码
            ResponseResult<User> inquire = userFeign.inquire(new UserSelect(null, phone));

            //根据电话号码获取人员信息
            if (inquire.getData() == null) {
                throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该电话号码不存在");
            }

            //获取人员信息中的id
            Integer Uid = inquire.getData().getId();

            //根据id找到供货商
            supplier supplier = getOne(new LambdaQueryWrapper<supplier>().eq(com.sand.entity.user.supplier::getUserid, Uid));

            //拼接
            return getSupplierDto(inquire.getData(), supplier);
        }

        //根据供货商表id查询
        supplier supplier = getById(id);
        if (supplier == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该客户不存在");
        }

        // 根据电话号码查询客户基本信息
        ResponseResult<User> inquire = userFeign.inquire(new UserSelect(null, phone));

        if (inquire.getData() == null) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID, "该电话号码不存在");
        }

        //拼接
        return getSupplierDto(inquire.getData(), supplier);
    }


    /**
     * 查询所有供货商
     */
    @Override
    public List<SupplierDto> listAll() {
        //获取所有供货商
        List<supplier> list = list();

        //存放所有供货商id
        Integer[] ids = new Integer[list.size()];

        //获取所有供货商的id
        for (int i = 0; i < list.size(); i++) {
            supplier supplier = list.get(i);
            Integer id = supplier.getUserid();
            ids[i] = id;
        }
        //根据供货商id查询供货商基本信息
        List<User> userList = userFeign.ids(ids).getData();

        //返回所有供货商
        List<SupplierDto> dtoList = new ArrayList<>();

        //拼接数据
        for (int i = 0; i < userList.size(); i++) {
            User user = userList.get(i);
            supplier supplier = list.get(i);
            SupplierDto supplierDto = getSupplierDto(user, supplier);
            dtoList.add(supplierDto);
        }

        //返回数据
        return dtoList;
    }

    /**
     * 用于拼接供货商和用户
     * @param user
     * @param supplier
     * @return
     */
    private static SupplierDto getSupplierDto(User user, supplier supplier) {
        SupplierDto supplierDto = new SupplierDto();

        BeanUtils.copyProperties(user,supplierDto);
        supplierDto.setUserid(supplier.getId());

        return supplierDto;
    }

}
